package com.lingxu.module.collection.service.impl;

import com.lingxu.base.common.util.DateUtil;
import com.lingxu.base.system.manage.vo.LoginUser;
import com.lingxu.module.collection.entity.*;
import com.lingxu.module.collection.mapper.CollectionMonitorMapper;
import com.lingxu.module.collection.service.ICollectionMonitorService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CollectionMonitorService implements ICollectionMonitorService {
    @Resource
    private CollectionMonitorMapper collectionMonitorMapper;
    @Override
    public CollectionMonitor getCollectionMonitorCountInfo(CollectionMonitorSearchVo vo) {
        String region = "";
        if(StringUtils.isBlank(vo.getRegion())){
            LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if(StringUtils.isNotBlank(user.getRegion())){
                region= user.getRegion();
            }else{
                region="320800";
            }
        }else {
            region= vo.getRegion();
        }

        CollectionMonitor collectionMonitor = new CollectionMonitor();
        String newdate = DateUtil.getDate("yyyy-MM-dd");
        Integer daycount = collectionMonitorMapper.getCollectionMonitorDayCount(newdate,region);
        Integer allcount = collectionMonitorMapper.getCollectionMonitorAllcount(region);
        Integer warncount = collectionMonitorMapper.getCollectionMonitorWarncount(newdate,region);
//        Integer warncount = collectionMonitorMapper.getCollectionMonitorWarncount();
        collectionMonitor.setDaycount(daycount);
        collectionMonitor.setAllcount(allcount);
        collectionMonitor.setWarncount(warncount);
        return collectionMonitor;
    }

    @Override
    public List<CollectionMonitorYear> getCollectionMonitorTimeInfo(CollectionMonitorSearchVo vo) {
        String year= vo.getYear();
        String region = "";
        LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(StringUtils.isNotBlank(user.getRegion())){
            region= user.getRegion();
        }else{
            region="320800";
        }
        return collectionMonitorMapper.getCollectionMonitorTimeInfo(year,region);
    }

    @Override
    public List<CollectionMonitorArea> getCollectionMonitorAreaInfo(CollectionMonitorSearchVo vo) {
        if(StringUtils.isBlank(vo.getRegion())){
                LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
                if(StringUtils.isNotBlank(user.getRegion())){
                    vo.setRegion(user.getRegion());
                }else{
                    vo.setRegion("320800");
                }
        }

//        String year=vo.getYear();
//        if("至今".equals(year)){
//            year=null;
//        }
//
        return collectionMonitorMapper.getCollectionMonitorAreaInfonew(vo);
    }
    @Override
    public List<CollectionMonitorArea> getWarnListAreaInfo(CollectionMonitorSearchVo vo) {
        if(StringUtils.isBlank(vo.getRegion())){
            LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if(StringUtils.isNotBlank(user.getRegion())){
                vo.setRegion(user.getRegion());
            }else{
                vo.setRegion("320800");
            }
        }
        return collectionMonitorMapper.getWarnListAreaInfo(vo);
    }

    @Override
    public String getRegionByCityName(String name) {
        return collectionMonitorMapper.getRegionByCityName(name);
    }

    @Override
    public String getCityNameByRegion(String region) {
        return collectionMonitorMapper.getCityNameByRegion(region);
    }

    @Override
    public List<CollectionMonitorUploadInfo> getCollectionMonitorList() {
        String region = "";
        LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(StringUtils.isNotBlank(user.getRegion())){
            region= user.getRegion();
        }else{
            region="320800";
        }
        return collectionMonitorMapper.getCollectionMonitorList(region);
    }
}
